﻿#region [ Code Modification History ]

//*******************************************************************************************************
//  GZipStreamTool.cs 
//
//  Code Modification History:
//  -----------------------------------------------------------------------------------------------------
//  15/01/2017 - V. Dennis Kholin
//       Generated original version of source code. 
//*******************************************************************************************************

#endregion

#region [ Using ]

using System;
using System.IO;
using System.IO.Compression;

#endregion

namespace GZipStreamTool.Base.GZipTools
{
    /// <summary>
    ///     GZipStreamTool
    /// </summary>
    public class GZipStreamTool
    {
        #region [ Members ]

        private static readonly int BUFFER_SIZE = 64 * 1024; // Kb

        #endregion

        #region [ Methods Static ]

        /// <summary>
        ///     Compress
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static byte[] Compress(byte[] inputData)
        {
            using (MemoryStream compressIntoMs = new MemoryStream())
            {
                using (BufferedStream gzs = new BufferedStream(new GZipStream(compressIntoMs, CompressionMode.Compress), BUFFER_SIZE))
                {
                    gzs.Write(inputData, 0, inputData.Length);
                }
                return compressIntoMs.ToArray();
            }
        }

        /// <summary>
        ///     Decompress
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static byte[] Decompress(byte[] inputData)
        {
            using (MemoryStream compressedMs = new MemoryStream(inputData))
            {
                using (MemoryStream decompressedMs = new MemoryStream())
                {
                    using (BufferedStream gzs = new BufferedStream(new GZipStream(compressedMs, CompressionMode.Decompress),BUFFER_SIZE))
                    {
                        gzs.CopyTo(decompressedMs);
                    }
                    return decompressedMs.ToArray();
                }
            }
        }
        /*
        /// <summary>
        ///     CompressTest
        /// </summary>
        /// <param name="fileToCompress"></param>
        public void Compress(FileInfo fileToCompress)
        {
            using (var originalFileStream = fileToCompress.OpenRead())
            {
                if (((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden) &
                    (fileToCompress.Extension != ".gz"))
                    using (var compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                    {
                        using (var compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                        {
                            originalFileStream.CopyTo(compressionStream);
                        }
                    }
            }
        } */

        #endregion
    }
}